package com.tgy.core.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgy.core.mapper.BlogArticleMapper;
import com.tgy.core.service.BlogArticleService;
import com.tgy.page.PageQuery;
import com.tgy.page.TableDataInfo;
import com.tgy.pojo.BlogArticle;
import com.tgy.result.LayuiTableResult;
import com.tgy.vo.StatisticsVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 文章信息Service业务层处理
 *
 * @author 自动生成
 * @date 2022-06-20
 */
@Service
public class BlogArticleServiceImpl extends ServiceImpl<BlogArticleMapper, BlogArticle> implements BlogArticleService {

    @Resource
    private BlogArticleMapper baseMapper;

    /**
     * 查询文章信息
     *
     * @param id 文章信息主键
     * @return 文章信息
     */
    @Override
    public BlogArticle queryById(String id){
        return baseMapper.selectById(id);
    }

    /**
     * 查询文章信息列表
     *
     * @param bo 文章信息
     * @return 文章信息
     */
    @Override
    public TableDataInfo<BlogArticle> queryPageList(BlogArticle bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogArticle> lqw = buildQueryWrapper(bo,pageQuery);
        Page<BlogArticle> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public LayuiTableResult queryLayuiPageList(BlogArticle bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogArticle> lqw = buildQueryWrapper(bo,pageQuery);
        Page<BlogArticle> result = baseMapper.selectPage(pageQuery.build(), lqw);
        return new LayuiTableResult(result.getTotal(),result.getRecords());
    }

    /**
     * 查询文章信息列表
     *
     * @param bo 文章信息
     * @return 文章信息
     */
    @Override
    public List<BlogArticle> queryList(BlogArticle bo) {
        LambdaQueryWrapper<BlogArticle> lqw = buildQueryWrapper(bo,null);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<BlogArticle> buildQueryWrapper(BlogArticle bo,PageQuery pageQuery) {
        LambdaQueryWrapper<BlogArticle> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getTitle()),BlogArticle::getTitle,bo.getTitle());
        lqw.like(StrUtil.isNotBlank(bo.getCategoryName()),BlogArticle::getCategoryName,bo.getCategoryName());
        lqw.eq(Objects.nonNull(bo.getOriginal()),BlogArticle::getOriginal,bo.getOriginal());
        lqw.eq(Objects.nonNull(bo.getStatus()),BlogArticle::getStatus,bo.getStatus());

        if (StrUtil.isNotBlank(bo.getCategoryId())){
            List<String> list = new ArrayList<>(Arrays.asList(bo.getCategoryId().split(",")));
            for (String s : list) {
                lqw.like(BlogArticle::getCategoryId,s);
            }
        }

        if (Objects.nonNull(pageQuery) && StrUtil.isNotBlank(pageQuery.getOrderByColumn())){
            boolean isAsc = true;
            if (pageQuery.getIsAsc().equals("desc")){
                isAsc = false;
            }
            if (pageQuery.getOrderByColumn().contains("top")){
                lqw.orderBy(Boolean.TRUE,isAsc,BlogArticle::getTop);
            }
            if (pageQuery.getOrderByColumn().contains("watchNumber")){
                lqw.orderBy(Boolean.TRUE,isAsc,BlogArticle::getWatchNumber);
            }
            if (pageQuery.getOrderByColumn().contains("createdTime")){
                lqw.orderBy(Boolean.TRUE,isAsc,BlogArticle::getCreatedTime);
            }
        }else {
            lqw.orderByDesc(BlogArticle::getCreatedTime);
        }

        if (CollectionUtil.isNotEmpty(bo.getExistFields())){
            // 排除掉某个字段
            for (String existField : bo.getExistFields()) {
                lqw.select(BlogArticle.class, info -> !info.getColumn().equals(existField));
            }
        }

        return lqw;
    }

    /**
     * 新增文章信息
     *
     * @param bo 文章信息
     * @return 结果
     */
    @Override
    public Boolean insertByBo(BlogArticle bo) {
        // validEntityBeforeSave(BlogArticle);
        boolean flag = baseMapper.insert(bo) > 0;
        if (flag) {
            bo.setId(bo.getId());
        }
        return flag;
    }

    /**
     * 修改文章信息
     *
     * @param bo 文章信息
     * @return 结果
     */
    @Override
    public Boolean updateByBo(BlogArticle bo) {
        // PBizPatient update = BeanUtil.toBean(bo, PBizPatient.class);
        // validEntityBeforeSave(BlogArticle);
        return baseMapper.updateById(bo) > 0;
    }

    @Override
    public Boolean deleteById(String id) {
        return baseMapper.deleteById(id) > 0;
    }


    /**
     * 批量删除文章信息
     *
     * @param ids 需要删除的文章信息主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        /*if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }*/
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public int updateStatus(String id, Integer status) {
        return baseMapper.updateStatus(id,status);
    }

    @Override
    public int addWatchNumber(String id) {
        return baseMapper.addWatchNumber(id);
    }

    @Override
    public int addCommentNumber(String id) {
        return baseMapper.addCommentNumber(id);
    }

    @Override
    public StatisticsVo getStatisticsNumber() {
        return baseMapper.getStatisticsNumber();
    }


}
